package com.rc.saas.tenant.service.finance;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.common.enums.RecordSourceEnum;
import com.rc.saas.tenant.mapper.rc.RcOperatorAccountRecordMapper;
import com.rc.saas.tenant.model.rc.RcOperatorAccountRecord;
import com.rc.saas.tenant.search.rc.RcOperatorAccountRecordSearch;
import com.rc.saas.tenant.vo.rc.ExportRcOperatorAccountRecordVo;
import com.rc.saas.tenant.vo.rc.RcOperatorAccountRecordData;
import com.rc.saas.tenant.vo.rc.RcOperatorAccountRecordStatistics;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
public class RcOperatorAccountRecordServiceImpl implements RcOperatorAccountRecordService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RcOperatorAccountRecordMapper rcOperatorAccountRecordMapper;


    @Override
    public RcOperatorAccountRecordMapper getMapper() {
        return rcOperatorAccountRecordMapper;
    }


    @Override
    public PageInfo<RcOperatorAccountRecord> searchByCondition(RcOperatorAccountRecordSearch rcOperatorAccountRecordSearch) {
        PageHelper.startPage(rcOperatorAccountRecordSearch.getOffset(), rcOperatorAccountRecordSearch.getLimit());
        List<RcOperatorAccountRecord> results = rcOperatorAccountRecordMapper.searchByCondition(rcOperatorAccountRecordSearch);
        return new PageInfo<RcOperatorAccountRecord>(results);
    }

    @Override
    public RcOperatorAccountRecordStatistics rcOperatorAccountRecordStatistics(RcOperatorAccountRecordSearch rcOperatorAccountRecordSearch) {
        return rcOperatorAccountRecordMapper.rcOperatorAccountRecordStatistics(rcOperatorAccountRecordSearch);
    }

    @Override
    public List<ExportRcOperatorAccountRecordVo> exportRcOperatorAccountRecord(RcOperatorAccountRecordSearch rcOperatorAccountRecordSearch) {
        return rcOperatorAccountRecordMapper.exportRcOperatorAccountRecord(rcOperatorAccountRecordSearch);
    }

    @Override
    public void updateTimeByPrimaryKey(RcOperatorAccountRecord info) {
        rcOperatorAccountRecordMapper.updateTimeByPrimaryKey(info);
    }

    @Override
    public void insertRcOperatorAccountRecord(RcOperatorAccountRecordData rcOperatorAccountRecordData) {
        RcOperatorAccountRecord info = new RcOperatorAccountRecord();
        if(null == rcOperatorAccountRecordData){
            //logger.info("用户：{}，未开通渠道商权益：{}", memberId, JSON.toJSONString(rcMemberFlag));
            logger.info("数据为空：资金交易流水明细数据录入失败");
            return;
        }
        if(null == rcOperatorAccountRecordData.getEntryId()){
            logger.info("收款方ID不能为空：entryId");
            return;
        }
        if(null == rcOperatorAccountRecordData.getEntryId()){
            logger.info("付款方ID不能为空：outId");
            return;
        }
        //默认全部微信入账，微信提现，开户行，银行卡信息留空
        BeanUtils.copyProperties(rcOperatorAccountRecordData, info);
        if(StringUtils.isBlank(info.getRecordSource())){
            info.setRecordSource(RecordSourceEnum.ONE.getCode());
        }
        info.setCreatedTime(new Date());
        info.setLastUpdatedTime(new Date());
        rcOperatorAccountRecordMapper.insertRcOperatorAccountRecord(info);
    }

}
